package com.anli1.Confog.ThreadPool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class syncThreadPoolq {

    @Bean("syncThreadPool")
    public ThreadPoolExecutor syncThreadPool(){
        int corePoolSize = 10; // 核心线程数索引加锁1-10
        int maxPoolSize = 10; // 最大线程数 --索引加锁1-10
        long keepAliveTime = 120; // 线程空闲时间
        TimeUnit unit = TimeUnit.SECONDS; // 时间单位
        // 指定拒绝策略为 DiscardPolicy
        RejectedExecutionHandler rejectedHandler = new ThreadPoolExecutor.DiscardPolicy();
        // 任务队列，使用SynchronousQueue容量为1，在没有线程去消费时不会保存任务
        ThreadPoolExecutor executor = new ThreadPoolExecutor
                (corePoolSize, maxPoolSize, keepAliveTime, unit,
                        new SynchronousQueue<>(),rejectedHandler);
        return executor;
    }
}
